

(* Mathematica Raw Program *)

(* Takes a subgraph and finds increasing neighborhoods of the subgraph upto distance d
   from the original subgraph vertices *)
NeighborhoodGraphs[g_, sg_, d_] := 
 Module[{graph = g,  subgraph = sg, distance = d}, 
  NestList[Function[u, NeighborhoodGraph[graph, u]], subgraph, 
   distance]]
   
   
 (* Split out the connected components from a graph into a list *)  
 ConnectedComponentsSplitOut[g_] :=
 	Module[{graph = g},
 		Map[Function[u,Subgraph[graph,u]],ConnectedComponents[graph]]
 	]
 	
 	
(* Count and reverse sort vertex list *)
CountSortedVerticesInList[gl__] :=
	Module[{graphlist = gl},
		Sort[Map[Function[u, Length[VertexList[u]]],graphlist],Greater]
	]

 (* Creates a descending sorted list of the fraction of vertices of the orginal graph *)	
 FractionOfOriginalGraph[g_, gl__] :=
 	Module[{graph = g, graphlist = gl},
 		CountSortedVerticesInList[graphlist] / Length[VertexList[graph]]
 	]
 	
 
 (* Some graph operations like UndirectedGraph do not bring over vertex properties this function copies edge properties over *)
 SetMissingVertexPropertiesFromSource[g1_,g2_] :=
 	Module[{graph1 = g1, graph2 = g2, vertexlist1,vertexlist2,vertex1,propertiestoset,i,j},
 		vertexlist1 = VertexList[graph1];
 		vertexlist2 = VertexList[graph2];
 		For[i=1, i <= Length[vertexlist1],i++,
 			vertex1 = vertexlist1[[i]];
 			If[VertexQ[graph2,vertex1],
	 			propertiestoset = Complement[PropertyList[{graph2,vertex1}],PropertyList[{graph1,vertex1}]];
	 			For[j=1, j <= Length[propertiestoset], j++,
	 				Set[PropertyValue[{graph1,vertex1},propertiestoset[[j]]],PropertyValue[{graph2,vertex1},propertiestoset[[j]]]] 		
 				]
 			] 
 		];
 		graph1
 	]


 (* Some graph operations like UndirectedGraph do not bring over edge properties this function copies edge properties over *) 	
 SetMissingEdgePropertiesFromSource[g1_,g2_] :=
 	Module[{graph1 = g1, graph2 = g2, edgelist1, edgelist2, edge1, edge2, propertiestoset,i,j,undirectedgraph1q,directedgraph2q,setconvertundirected},
 		edgelist1 = EdgeList[graph1];
 		edgelist2 = EdgeList[graph2];
 		undirectedgraph1q = UndirectedGraphQ[graph1];
 		directedgraph2q = DirectedGraphQ[graph2];
 		If[undirectedgraph1q && directedgraph2q, setconvertundirected = True,setconvertundirected = False];
 		For[i=1, i <= Length[edgelist1],i++,
 			edge1 = edgelist1[[i]];
 			If[setconvertundirected,
 				edge2 = EdgeList[DirectedGraph[Subgraph[graph1,{edge1}]]][[1]],
 				edge2 = edge1;
 			];
 			If[EdgeQ[graph2,edge2],
	 			propertiestoset = Complement[PropertyList[{graph2,edge2}],PropertyList[{graph1,edge1}]];
	 			For[j=1, j <= Length[propertiestoset], j++,
	 				Set[PropertyValue[{graph1,edge1},propertiestoset[[j]]],PropertyValue[{graph2,edge2},propertiestoset[[j]]]] 		
 				]
 			] 
 		];
 		graph1
 	]
 
(* Given a table consisting of a header row and body align values in the graph to the function *) 
AddAdditionalPropertiesFromTable[g_, ivfmg_, t_, ifld_, fta_] :=
	Module[{graph = g, table = t, idfield = ifld, fieldtoadd = fta, idfieldfromgraph = ivfmg, idvaluefromgraph,
		header, body, locationfieldvalue,locationidvalue, 
		lookupfunc,i,vertexlist,u},
		header = table[[1]];
		body = table[[2;;]];
		
		locationfieldvalue = Position[header,fieldtoadd][[1]][[1]];
		locationidvalue = Position[header,idfield][[1]][[1]];
		
		For[i=1, i <= Length[body],i++,
			u = body[[i]];
			lookupfunc[u[[locationidvalue]]] := u[[locationfieldvalue]]];
			
		vertexlist = VertexList[graph];
		For[i=1, i <= Length[vertexlist], i++,
			idvaluefromgraph = PropertyValue[{graph,vertexlist[[i]]},idfieldfromgraph];
			If[idvaluefromgraph === $Failed, Null,
				Set[PropertyValue[{graph,vertexlist[[i]]},fieldtoadd],lookupfunc[idvaluefromgraph]];
			]
		];
		graph
	]
	
	
SetVertexPropertyFromOtherProperty[g_, p1_, p2_, f_] :=
	Module[{graph=g,property1=p1, property2 = p2, function2apply=f, vertexlist,i,property1value},
		vertexlist = VertexList[graph];
		For[i=1, i <= Length[vertexlist], i++,
			property1value = PropertyValue[{graph,vertexlist[[i]]},property1];
			If[property1value === $Failed, Null,
				Set[PropertyValue[{graph,vertexlist[[i]]},property2],function2apply[property1value]];
			]
		];
		graph
	] 
			
	
 		
